package com.zl.tby.core.base.bean;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.model.TbySpeciesCategory;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author changpeng
 * @version 1.0
 * @since 2024/8/9 15:03
 */
@Data
public class OptionType {
    /**唯一值*/
    private Object value;
    /**显示的值*/
    private String label;
    /**上级*/
    private Object parentId;
    /**子集*/
    private List<OptionType> children;

    public OptionType(Object value, String label) {
        this.value = value;
        this.label = label;
        this.children = new ArrayList<>();
    }

    public OptionType(Object value, String label, List<OptionType> children) {
        this.value = value;
        this.label = label;
        this.children = children;
    }

    public static List<OptionType> recurOptions(Object parentId, List<OptionType> dataList) {
        List<OptionType> resultList = ToolsUtil.emptyIfNull(dataList).stream()
                .filter(bean -> bean.getParentId().toString().equals(parentId.toString()))
                .map(bean -> new OptionType(bean.getValue(), bean.getLabel(), recurOptions(bean.getValue(), dataList)))
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        return resultList;
    }
}
